﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Soft
{
    public static  class JsonConvertHelper
    {

        /// <summary>
        /// 使用newtownsoft 反序列化为T对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T FormJson<T>(string json)
        {
            return  JsonConvert.DeserializeObject<T>(json);
        }


        /// <summary>
        /// 使用newtownsoft 反序列化为object对象
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static object FormJson(string json)
        {
            return JsonConvert.DeserializeObject(json);
        }


        /// <summary>
        /// 使用newtownsoft 反序列化为object对象
        /// </summary>
        /// <param name="json"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static object FormJson(string json, Type type)
        {
            return JsonConvert.DeserializeObject(json, type);
        }


        /// <summary>
        /// 将Object序列化为 字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJson(object obj)
        {
            return JsonConvert.SerializeObject(obj, new JsonConverter[]
            {
                JsonConvertHelper._TimeFormat
            });
        }


        /// <summary>
        /// 日期格式化
        /// </summary>
        private static readonly IsoDateTimeConverter _TimeFormat = new IsoDateTimeConverter
        {
            DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff"
        };


        /// <summary>
        /// json为空对象
        /// </summary>
        public static readonly string JsonNull = JsonConvertHelper.ToJson(null);

   
    }
}
